package com.gluck.cases.core.block.impl;

import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gluck.cases.common.utils.enums.BizSerErrorEnum;
import com.gluck.cases.common.utils.utils.AssertUtil;
import com.gluck.cases.core.block.AdminUserBlock;
import com.gluck.cases.common.utils.dal.dao.AdminUserMapper;
import com.gluck.cases.core.modal.assemble.AdminUserAssemble;
import com.gluck.cases.core.modal.dto.AdminUserDTO;
import com.gluck.cases.core.modal.entity.AdminUser;
import com.gluck.cases.core.modal.req.AdminUserSearchReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @ClassName AdminUserBlockImpl
 * @Desc AdminUser block实现类
 * @Author I am Gluck
 * @Date 2022/5/15 - 10:09 下午
 **/
@Service
public class AdminUserBlockImpl implements AdminUserBlock {

    @Autowired
    private AdminUserMapper adminUserMapper;

    @Override
    public boolean createUser(AdminUserDTO user) {
        AdminUser adminUser = AdminUserAssemble.adminUserDTOConvertAdminUser(user);
        int insert = adminUserMapper.insert(adminUser);
        return insert > 0;
    }

    @Override
    public AdminUser updateUser(AdminUserDTO user) {
        AdminUser adminUser = adminUserMapper.selectByUsername(user.getUsername());
        AssertUtil.isTrue(Objects.isNull(adminUser), BizSerErrorEnum.SELECT_ERROR);
        AdminUser newAdminUser = AdminUserAssemble.getUpdateAdminUserByDTO(user, adminUser);
        AssertUtil.isTrue((adminUserMapper.updateAdminUserById(newAdminUser) == 0 ? true : false), BizSerErrorEnum.UPDATE_ERROR);
        return newAdminUser;
    }

    @Override
    public boolean deleteUser(Long userId) {
        int delete = adminUserMapper.deleteById(userId);
        return delete > 0;
    }

    @Override
    public boolean changePassword(Long userId, String newPassword) {
        AdminUser adminUser = new AdminUser();
        adminUser.setId(userId);
        adminUser.setPassword(newPassword);
        int update = adminUserMapper.updatePassowrdById(adminUser);
        return update > 0;
    }

    @Override
    public AdminUser findOne(Long userId) {
        return adminUserMapper.selectById(userId);
    }

    @Override
    public List<AdminUser> findAll() {
        return adminUserMapper.queryAll();
    }

    @Override
    public PageInfo<AdminUser> queryListByPageHelper(Integer startPage, Integer endPage) {
        PageInfo<AdminUser> adminUsers = new PageInfo<>(PageHelper.startPage(startPage, endPage).doSelectPage(() -> adminUserMapper.queryAll()));
        return adminUsers;
    }

    @Override
    public AdminUser queryByUsername(String username) {
        return adminUserMapper.selectByUsername(username);
    }

    @Override
    public AdminUser queryUser(String username, String password) {
        AdminUser adminUser = adminUserMapper.queryUser(username, password);
        return adminUser;
    }

    @Override
    public PageInfo<AdminUser> searchUsers(AdminUserSearchReq req) {
        PageInfo<AdminUser> adminUsers = new PageInfo<>(PageHelper.startPage(req.getPage(), req.getSize()).doSelectPage(new ISelect() {
            @Override
            public void doSelect() {
                adminUserMapper.search(req);
            }
        }));
        return adminUsers;
    }
}
